package com.cn.zsc.service;

import com.cn.zsc.dblocal.dao.OrderMapper;
import com.cn.zsc.dblocal.model.Order;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component("messageListener")
public class ChangeOrderStatus implements MessageListenerConcurrently {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        if (list == null || list.size() == 0){
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
        for (MessageExt messageExt : list){
            String orderId = messageExt.getMsgId();
            String msg = new String(messageExt.getBody());
            System.out.println("msg="+msg);
            Order order = orderMapper.selectByPrimaryKey(Integer.parseInt(orderId));
            if (order == null) return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            try{
                order.setOrderStatus(1); // 已支付
                order.setUpdateTime(new Date());
                order.setUpdateUser(0);
                orderMapper.updateByPrimaryKey(order);
            }catch (Exception ex){
                ex.printStackTrace();
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }

        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}
